<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Console\Commands;

use App\Models\User;
use App\Service\Notifications\NotificationService;
use Illuminate\Console\Command;

class ClearTheMembershipLevel extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'ClearTheMembershipLevel:name';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '清除会员级别';

    /**
     * Create a new command instance.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $notify = NotificationService::make();
        User::with('pointAccount')
            ->where('member_level', '>', 0)->chunkById(100, function ($users) use ($notify) {
                $users->each(function ($user) use ($notify) {
                    if ($user->pointAccount->available_points <= 0) {
                        $user->member_level = 0;
                        $user->save();
                        $notify->SystemNotify($user, '您积分为0 拼单等级已重置');
                    }
                });
            });
    }
}
